package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.SExaminationInformation;
import com.zhentao.domain.SRetakesInfo;
import com.zhentao.domain.dto.RetakesInfoDto;
import com.zhentao.mapper.SExaminationInformationMapper;
import com.zhentao.result.Result;
import com.zhentao.service.SRetakesInfoService;
import com.zhentao.mapper.SRetakesInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* @author 86150
* @description 针对表【s_retakes_info】的数据库操作Service实现
* @createDate 2024-08-02 08:38:16
*/
@Service
public class SRetakesInfoServiceImpl extends ServiceImpl<SRetakesInfoMapper, SRetakesInfo>
    implements SRetakesInfoService{

    @Resource
    private SRetakesInfoMapper retakesInfoMapper;

    @Resource
    private SExaminationInformationMapper sExaminationInformationMapper;


    //老师提交补考信息
    @Override
    public Result submitRetakesInfo(SRetakesInfo retakesInfo) {
        int insert = retakesInfoMapper.insert(retakesInfo);
        return Result.OK(insert);
    }

    //管理员查看补考信息
    @Override
    public Result getRetakesInfo(RetakesInfoDto retakesInfoDto) {
        Page<SRetakesInfo> page=new Page<>(retakesInfoDto.getPageNum(),retakesInfoDto.getPageSize());
        Page<SRetakesInfo> sRetakesInfoPage = retakesInfoMapper.getRetakesInfo(page, retakesInfoDto);
        return Result.OK(sRetakesInfoPage);
    }


    //管理员审核补考通过
    @Override
    public Result approved(Integer rid) {
        SRetakesInfo sRetakesInfo = this.retakesInfoMapper.selectById(rid);
        if (sRetakesInfo.getStatus()==0){
            sRetakesInfo.setStatus(1);
            this.retakesInfoMapper.updateById(sRetakesInfo);
        }
        SExaminationInformation sExaminationInformation = this.sExaminationInformationMapper.selectById(sRetakesInfo.getExaminationId());
        sExaminationInformation.setBeginTime(sRetakesInfo.getRetakesBegin());
        sExaminationInformation.setEndTime(sRetakesInfo.getRetakesEnd());
        // 计算两个 Date 对象之间的时间差（毫秒）
        long differenceInMillis = sExaminationInformation.getEndTime().getTime() - sExaminationInformation.getBeginTime().getTime();
        // 转换为小时、分钟、秒
        long differenceInSeconds = TimeUnit.MILLISECONDS.toSeconds(differenceInMillis);
        long hours = TimeUnit.SECONDS.toHours(differenceInSeconds);
        long minutes = TimeUnit.SECONDS.toMinutes(differenceInSeconds) % 60;
        long seconds = differenceInSeconds % 60;
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        // 设置小时、分钟和秒
        calendar.set(Calendar.HOUR_OF_DAY, (int) hours);
        calendar.set(Calendar.MINUTE, (int) minutes);
        calendar.set(Calendar.SECOND, (int) seconds);
        // 获取 Date 对象
        Date date = calendar.getTime();
        sExaminationInformation.setExamDuration(date);
        sExaminationInformation.setState(0);
        this.sExaminationInformationMapper.updateById(sExaminationInformation);
        return Result.OK("审核通过");
    }



    //管理员审核补考不通过
    @Override
    public Result refuse(Integer rid, String backCause) {
        SRetakesInfo sRetakesInfo = this.retakesInfoMapper.selectById(rid);
        if (sRetakesInfo.getStatus()==0){
            sRetakesInfo.setStatus(2);
            sRetakesInfo.setBackCause(backCause);
            this.retakesInfoMapper.updateById(sRetakesInfo);
        }
        return Result.OK("已驳回");
    }

    //老师查看自己提交的补考信息
    @Override
    public Result getRetakesInfoByTeacher(Integer tid, RetakesInfoDto retakesInfoDto) {
        Page<SRetakesInfo> page=new Page<>(retakesInfoDto.getPageNum(),retakesInfoDto.getPageSize());
        Page<SRetakesInfo> sRetakesInfoPage = retakesInfoMapper.getRetakesInfoByTeacher(page, tid, retakesInfoDto);
        return Result.OK(sRetakesInfoPage);
    }


}




